<?php

namespace app\beautiful\model\order;

use app\beautiful\model\member\MemberModel;
use app\common\model\CommonModel;
use think\facade\Db;


class PinTuanOrderModel extends CommonModel{

    protected  $name = 'beautiful_pin_tuan_order';

    protected  $pk   = 'pin_tuan_order_id';

    protected  $type = ['pay_info'         => 'array'  ];

    public function item()

    {

        return $this->hasMany(PinTuanOrderItemModel::class,'pin_tuan_order_id','pin_tuan_order_id');

    }
    public function oneitem()

    {

        return $this->hasOne(PinTuanOrderItemModel::class,'pin_tuan_order_id','pin_tuan_order_id');

    }

    public function member()

    {

        return $this->hasOne(MemberModel::class,'member_id','member_id');

    }

    public function skus(){

        return  $this->hasMany(PinTuanOrderItemModel::class,'pin_tuan_order_id','pin_tuan_order_id');

    }
    

    public function getGroupDjsTimeAttr($value,$data)

    {

        $djs_time=$data['add_time']+86400-time();//一天有效

        return $djs_time>0 ? $djs_time : 0;

    }

    

    public function getDjsTimeAttr($value,$data){

        $t = time();

        $djs = $data['add_time'] + 1800 - $t;

        return  $djs > 0 ?  $djs : 0;

    }



    public function getStatusMeansAttr($value,$data)

    {

        $status_means=[

            1=>'待付款',

            2=>'待成团',

            3=>'已成团',

            -1=>'拼团失败-带退款',

            -2=>'拼团失败-已退款',

            -3=>'取消拼团'

        ];



        return $status_means[$data['status']] ?? '状态异常';

    }


    public function getPayTimeFormatAttr($value,$data){

        return $data['pay_time'] ? date('Y-m-d H:i:s',$data['pay_time']) : '-';

    }



    public function getOrderTimeAttr($value,$data){

        return date('Y-m-d H:i:s',$data['add_time']);

    }

    public function getPayTypeMeanAttr($value,$data)

    {
        $type_means=[
            'weixin'=>'微信支付',
            'platform_money'=>'钱包支付',
            'money'=>'储值卡支付',
            'offline' => '代客下单',
        ];
        if(isset($data['mix_pay_type']) && $data['mix_pay_type']){
            $mix_pay_type = explode(',',$data['mix_pay_type']);
            $str = '';
            for($i=0;$i<count($mix_pay_type);$i++){
                $pay_type = $mix_pay_type[$i];
                if($type_means[$pay_type]){
                    $str.=$type_means[$pay_type].',';
                }
            }
            $str = trim($str,",");
            return $str ?? '-';
        }else{
            return $type_means[$data['pay_type']] ?? '-';
        }

        return $type_means[$data['pay_type']] ?? '-';

    }

    public static function user_order($where){
        $query = Db::name('beautiful_pin_tuan_order')->alias('a')
            ->join('beautiful_member b','a.member_id=b.member_id')
            ->where(['a.from_type'=>2])
            ->whereIn('a.status',[3])
            ->when(!empty($where['pin_tuan_order_id']),function ($query) use ($where){
                $query->where(['a.pin_tuan_order_id'=>$where['pin_tuan_order_id']]);
            })
            ->when(!empty($where['member_id']),function ($query) use ($where){
                $query->where(['a.member_id'=>$where['member_id']]);
            })
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['a.shop_id'=>$where['shop_id']]);
            })
            ->when(!empty($where['pid']),function ($query) use ($where){
                $query->where(['b.pid'=>$where['pid']]);
            })
            ->when(!empty($where['start_time']),function ($query) use ($where){
                $query->where('a.pay_time','>=',$where['start_time']);
            })
            ->when(!empty($where['end_time']),function ($query) use ($where){
                $query->where('a.pay_time','<=',$where['end_time']);
            })
            ->field("sum(a.need_pay) as money,count(a.pin_tuan_order_id) as all_num,sum(a.all_goods_gross) as all_goods_gross,a.member_id,b.nick_name");
        $info=$query->find();
        return $info;
    }
    public static function list($where,$order,$page=1,$limit=10){
        $query = Db::name('beautiful_pin_tuan_order')->alias('a')
            ->where(['a.from_type'=>2])
            ->when(!empty($where['pay_time']),function ($query) use ($where){
                $query->where('a.pay_time','>',$where['pay_time']);
            })
            ->field("a.*");
        $list=$query->order($order)
            ->limit($limit)->page($page)
            ->select()
            ->toArray();
        return $list;
    }
}



